/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.naizhuai.ui.adapter.business;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.naizhuai.R;
import com.naizhuai.ui.BaseActivity;
import com.naizhuai.ui.entity.SimpleKeyValueBean;
import com.naizhuai.util.DeviceUtils;
import com.naizhuai.util.ImageUtils;

import java.io.File;
import java.util.List;

public class ActionBrightPointImageAdapterHolder extends RecyclerView.Adapter {

    private OnItemClickListener mItemClickListener;
    private BaseActivity mContext;
    private List<String> filePathStrs;
    private int pagerWidth;


    public ActionBrightPointImageAdapterHolder(BaseActivity context, List<String> filePathStrs, int pagerWidth) {
        this.mContext = context;
        this.filePathStrs = filePathStrs;
        this.pagerWidth = pagerWidth;
    }


    @Override
    public int getItemCount() {
        return filePathStrs.size();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_action_bright_point_image_details, parent, false);
        return new ActionImageItemHolder(sView);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ActionImageItemHolder imageHolder = (ActionImageItemHolder) holder;
        imageHolder.itemView.post(new Runnable() {
            @Override
            public void run() {
                RecyclerView.LayoutParams params= (RecyclerView.LayoutParams) imageHolder.itemView.getLayoutParams();
                int itemWidth=(pagerWidth-params.rightMargin*2)/3;
                params.height=itemWidth;
                params.width=itemWidth;
                imageHolder.itemView.setLayoutParams(params);
                ImageUtils.glideImageViewByUrl(mContext,imageHolder.image,filePathStrs.get(position));
            }
        });

    }


    public class ActionImageItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView image;

        public ActionImageItemHolder(View view) {
            super(view);
            image = (ImageView) view.findViewById(R.id.image_details);
            view.getLayoutParams().width = DeviceUtils.getScreenDevice(mContext)[0] / 3;
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null)
                mItemClickListener.onItemClick(v, getAdapterPosition());
        }
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

}
